package com.forlove.app.manager.async;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LocationUtils;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.NetworkUtils;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.event.EventEntity;
import com.forlove.app.manager.DataManager;
import com.forlove.app.manager.EventManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.login.entity.GpsEntity;
import com.forlove.app.qselect.login.entity.LimitEntity;

import org.greenrobot.eventbus.EventBus;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/21.
 */

public class LoadGPSAsync extends AsyncTaskLoader<GpsEntity> {
    public static final int ASYNC_ID = 20008;
    private AMapLocationClientOption locationOption = null;
    private AMapLocationClient locationClient = null;
    private GpsEntity gpsEntity = null;

    public LoadGPSAsync(Context context) {
        super(context);
    }

    @Override
    public GpsEntity loadInBackground() {

        final WeakReference<Context> contextWeakReference = new WeakReference<Context>(getContext());
        locationClient = new AMapLocationClient(contextWeakReference.get());
        locationOption = LocationUtils.getDefaultOption();
        //设置定位参数
        locationClient.setLocationOption(locationOption);
        locationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation location) {
                if (location != null) {
                    if (location.getErrorCode() == 0
                            && NetworkUtils.instance(contextWeakReference.get()).isNetworkAvailable()) {
                        locationClient.stopLocation();
                        locationClient.onDestroy();
                        LogUtil.e("yjl", "location:" + location.getAddress());
                        gpsEntity = new GpsEntity();
                        gpsEntity.lng = String.valueOf(location.getLongitude());
                        gpsEntity.lat = String.valueOf(location.getLatitude());
                        DataManager.saveLocaition(contextWeakReference.get(), gpsEntity);
                        Map param = new HashMap();
                        param.put("lat", gpsEntity.lat);
//                        param.put("lat", "0.0");
                        param.put("lng", gpsEntity.lng);
//                        param.put("lng", "0.0");
                        try {
                            NetworkManager.getInstance().loadDataByPost(ServerManager.ACTION_LIMIT_LOCATION, param, new ICallBack() {
                                @Override
                                public void onSuccess(int statusCode, String result) {
                                    LimitEntity limitEntity = ParseHelper.getModel(result, "data", LimitEntity.class);
                                    LogUtil.e("yjl", "运营限制:" + result);
                                    if (!limitEntity.canlogin) {
                                        EventEntity eventEntity = new EventEntity();
                                        eventEntity.eventId = EventManager.EVENT_LIMIT_LOGIN;
                                        eventEntity.eventName = "limit_login";
                                        EventBus.getDefault().post(eventEntity);
                                    }
                                }

                                @Override
                                public void onFail(int statusCode, String result) {

                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    } else {
                        LogUtil.e("yjl", "获取gps失败");
                        EventEntity eventEntity = new EventEntity();
                        eventEntity.eventId = EventManager.EVENT_LIMIT_LOGIN;
                        eventEntity.eventName = "limit_login";
                        EventBus.getDefault().post(eventEntity);
                    }
                }
            }
        });
        locationClient.startLocation();
        return gpsEntity;
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }

    @Override
    protected void onStopLoading() {
        super.onStopLoading();
        locationClient.stopLocation();
        locationClient.onDestroy();
    }
}
